#include "zf_common_headfile.h"

small_device_value_struct motor_value;      // 瀹氫箟閫氳鍙傛暟缁撴瀯浣�


//-------------------------------------------------------------------------------------------------------------------
// 鍑芥暟绠�浠�     鏃犲埛椹卞姩 涓插彛鎺ユ敹鍥炶皟鍑芥暟
// 鍙傛暟璇存槑     void
// 杩斿洖鍙傛暟     void
// 浣跨敤绀轰緥     uart_control_callback(1000, -1000);
// 澶囨敞淇℃伅     鐢ㄤ簬瑙ｆ瀽鎺ユ敹鍒扮殑閫熷害鏁版嵁  璇ュ嚱鏁伴渶瑕佸湪瀵瑰簲鐨勪覆鍙ｆ帴鏀朵腑鏂腑璋冪敤
//-------------------------------------------------------------------------------------------------------------------
void uart_control_callback(void)
{
    uint8 receive_data;                                                                     // 瀹氫箟涓存椂鍙橀噺

    if(uart_query_byte(SMALL_DRIVER_UART, &receive_data))                                   // 鎺ユ敹涓插彛鏁版嵁
    {
        if(receive_data == 0xA5 && motor_value.receive_data_buffer[0] != 0xA5)              // 鍒ゆ柇鏄惁鏀跺埌甯уご 骞朵笖 褰撳墠鎺ユ敹鍐呭涓槸鍚︽纭寘鍚抚澶�
        {
            motor_value.receive_data_count = 0;                                             // 鏈敹鍒板抚澶存垨鑰呮湭姝ｇ‘鍖呭惈甯уご鍒欓噸鏂版帴鏀�
        }

        motor_value.receive_data_buffer[motor_value.receive_data_count ++] = receive_data;  // 淇濆瓨涓插彛鏁版嵁

        if(motor_value.receive_data_count >= 7)                                             // 鍒ゆ柇鏄惁鎺ユ敹鍒版寚瀹氭暟閲忕殑鏁版嵁
        {
            if(motor_value.receive_data_buffer[0] == 0xA5)                                  // 鍒ゆ柇甯уご鏄惁姝ｇ‘
            {

                motor_value.sum_check_data = 0;                                             // 娓呴櫎鏍￠獙浣嶆暟鎹�

                for(int i = 0; i < 6; i ++)
                {
                    motor_value.sum_check_data += motor_value.receive_data_buffer[i];       // 閲嶆柊璁＄畻鏍￠獙浣�
                }

                if(motor_value.sum_check_data == motor_value.receive_data_buffer[6])        // 鏍￠獙鏁版嵁鍑嗙‘鎬�
                {

                    if(motor_value.receive_data_buffer[1] == 0x02)                          // 鍒ゆ柇鏄惁姝ｇ‘鎺ユ敹鍒� 閫熷害杈撳嚭 鍔熻兘瀛�
                    {
                        motor_value.receive_left_speed_data  = (((int)motor_value.receive_data_buffer[2] << 8) | (int)motor_value.receive_data_buffer[3]);  // 鎷熷悎宸︿晶鐢垫満杞�熸暟鎹�

                        motor_value.receive_right_speed_data = -(((int)motor_value.receive_data_buffer[4] << 8) | (int)motor_value.receive_data_buffer[5]);  // 鎷熷悎鍙充晶鐢垫満杞�熸暟鎹�
                    }

                    motor_value.receive_data_count = 0;                                     // 娓呴櫎缂撳啿鍖鸿鏁板��

                    memset(motor_value.receive_data_buffer, 0, 7);                          // 娓呴櫎缂撳啿鍖烘暟鎹�
                }
                else
                {
                    motor_value.receive_data_count = 0;                                     // 娓呴櫎缂撳啿鍖鸿鏁板��

                    memset(motor_value.receive_data_buffer, 0, 7);                          // 娓呴櫎缂撳啿鍖烘暟鎹�
                }
            }
            else
            {
                motor_value.receive_data_count = 0;                                         // 娓呴櫎缂撳啿鍖鸿鏁板��

                memset(motor_value.receive_data_buffer, 0, 7);                              // 娓呴櫎缂撳啿鍖烘暟鎹�
            }
        }
    }
}

//-------------------------------------------------------------------------------------------------------------------
// 鍑芥暟绠�浠�     鏃犲埛椹卞姩 璁剧疆鐢垫満鍗犵┖姣�
// 鍙傛暟璇存槑     left_duty       宸︿晶鐢垫満鍗犵┖姣�  鑼冨洿 -10000 ~ 10000  璐熸暟涓哄弽杞�
// 鍙傛暟璇存槑     right_duty      鍙充晶鐢垫満鍗犵┖姣�  鑼冨洿 -10000 ~ 10000  璐熸暟涓哄弽杞�
// 杩斿洖鍙傛暟     void
// 浣跨敤绀轰緥     small_driver_set_duty(1000, -1000);
// 澶囨敞淇℃伅
//-------------------------------------------------------------------------------------------------------------------
void small_driver_set_duty(int16 left_duty, int16 right_duty)
{
    motor_value.send_data_buffer[0] = 0xA5;                                         // 閰嶇疆甯уご

    motor_value.send_data_buffer[1] = 0X01;                                         // 閰嶇疆鍔熻兘瀛�

    motor_value.send_data_buffer[2] = (uint8)((left_duty & 0xFF00) >> 8);           // 鎷嗗垎 宸︿晶鍗犵┖姣� 鐨勯珮鍏綅

    motor_value.send_data_buffer[3] = (uint8)(left_duty & 0x00FF);                  // 鎷嗗垎 宸︿晶鍗犵┖姣� 鐨勪綆鍏綅

    motor_value.send_data_buffer[4] = (uint8)((right_duty & 0xFF00) >> 8);          // 鎷嗗垎 鍙充晶鍗犵┖姣� 鐨勯珮鍏綅

    motor_value.send_data_buffer[5] = (uint8)(right_duty & 0x00FF);                 // 鎷嗗垎 鍙充晶鍗犵┖姣� 鐨勪綆鍏綅

    motor_value.send_data_buffer[6] = 0;                                            // 鍜屾牎楠屾竻闄�

    for(int i = 0; i < 6; i ++)
    {
        motor_value.send_data_buffer[6] += motor_value.send_data_buffer[i];         // 璁＄畻鏍￠獙浣�
    }

    uart_write_buffer(SMALL_DRIVER_UART, motor_value.send_data_buffer, 7);                     // 鍙戦�佽缃崰绌烘瘮鐨� 瀛楄妭鍖� 鏁版嵁
}

//-------------------------------------------------------------------------------------------------------------------
// 鍑芥暟绠�浠�     鏃犲埛椹卞姩 鑾峰彇閫熷害淇℃伅
// 鍙傛暟璇存槑     void
// 杩斿洖鍙傛暟     void
// 浣跨敤绀轰緥     small_driver_get_speed();
// 澶囨敞淇℃伅     浠呴渶鍙戦�佷竴娆� 椹卞姩灏嗗懆鏈熷彂鍑洪�熷害淇℃伅(榛樿10ms)
//-------------------------------------------------------------------------------------------------------------------
void small_driver_get_speed(void)
{
    motor_value.send_data_buffer[0] = 0xA5;                                         // 閰嶇疆甯уご

    motor_value.send_data_buffer[1] = 0X02;                                         // 閰嶇疆鍔熻兘瀛�

    motor_value.send_data_buffer[2] = 0x00;                                         // 鏁版嵁浣嶆竻绌�

    motor_value.send_data_buffer[3] = 0x00;                                         // 鏁版嵁浣嶆竻绌�

    motor_value.send_data_buffer[4] = 0x00;                                         // 鏁版嵁浣嶆竻绌�

    motor_value.send_data_buffer[5] = 0x00;                                         // 鏁版嵁浣嶆竻绌�

    motor_value.send_data_buffer[6] = 0xA7;                                         // 閰嶇疆鏍￠獙浣�

    uart_write_buffer(SMALL_DRIVER_UART, motor_value.send_data_buffer, 7);                     // 鍙戦�佽幏鍙栬浆閫熸暟鎹殑 瀛楄妭鍖� 鏁版嵁
}


//-------------------------------------------------------------------------------------------------------------------
// 鍑芥暟绠�浠�     鏃犲埛椹卞姩 鍙傛暟鍒濆鍖�
// 鍙傛暟璇存槑     void
// 杩斿洖鍙傛暟     void
// 浣跨敤绀轰緥     small_driver_init();
// 澶囨敞淇℃伅
//-------------------------------------------------------------------------------------------------------------------
void small_driver_init(void)
{
    memset(motor_value.send_data_buffer, 0, 7);                             // 娓呴櫎缂撳啿鍖烘暟鎹�

    memset(motor_value.receive_data_buffer, 0, 7);                          // 娓呴櫎缂撳啿鍖烘暟鎹�

    motor_value.receive_data_count          = 0;

    motor_value.sum_check_data              = 0;

    motor_value.receive_right_speed_data    = 0;

    motor_value.receive_left_speed_data     = 0;
}


//-------------------------------------------------------------------------------------------------------------------
// 鍑芥暟绠�浠�     鏃犲埛椹卞姩 涓插彛閫氳鍒濆鍖�
// 鍙傛暟璇存槑     void
// 杩斿洖鍙傛暟     void
// 浣跨敤绀轰緥     small_driver_uart_init();
// 澶囨敞淇℃伅
//-------------------------------------------------------------------------------------------------------------------
void small_driver_uart_init(void)
{
    uart_init(SMALL_DRIVER_UART, SMALL_DRIVER_BAUDRATE, SMALL_DRIVER_RX, SMALL_DRIVER_TX);      // 涓插彛鍒濆鍖�

    uart_rx_interrupt(SMALL_DRIVER_UART, 1);                                                    // 浣胯兘涓插彛鎺ユ敹涓柇

    small_driver_init();                                                                        // 缁撴瀯浣撳弬鏁板垵濮嬪寲

    small_driver_set_duty(0, 0);                                                                // 璁剧疆0鍗犵┖姣�

    small_driver_get_speed();                                                                   // 鑾峰彇瀹炴椂閫熷害鏁版嵁
}














